<template>
  <div class="app-container customer-div">

    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="关键字：">
          <el-input v-model="listQuery.keywords" placeholder="司机姓名/常跑路线/手机号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
                  @click="handleSearchList()"
                  type="primary">
            查询
          </el-button>
            <el-button
                    @click="showSendForm('add')"
                    type="primary">
                新增
            </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--数据列表-->
    <div class="table-container">
      <el-table
              v-loading="listLoading"
              :data="tableData"
              border
              style="width: 100%; margin-top: 20px">
        <el-table-column  label="司机姓名" align="center" prop="name">
        </el-table-column>
        <el-table-column  label="司机手机号"  align="center" prop="phone">
        </el-table-column>
        <el-table-column  label="车牌号"  prop="carNo" align="center">
        </el-table-column>
          <el-table-column  label="身份证号" align="center" prop="idCard">
          </el-table-column>
          <el-table-column  label="银行名称" align="center" prop="bankName">
          </el-table-column>
          <el-table-column
                  prop="bankNo"
                  label="银行卡号"
                  align="center">
          </el-table-column>
          <el-table-column
                  prop="route"
                  label="常跑路线"
                  width="200">
          </el-table-column>
        <el-table-column
                fixed="right"
                label="操作"
                align="center">
          <template slot-scope="scope">
                <el-button
                        type="text"
                        size="mini"
                        @click="showSendForm('edit',scope.row)">编辑
                </el-button>
              <el-button
                      type="text"
                      size="mini"
                      @click="delDate(scope.row)">删除
              </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              layout="total, sizes,prev, pager, next,jumper"
              :current-page.sync="listQuery.pageNum"
              :page-size="listQuery.pageSize"
              :page-sizes="[10,15,20]"
              :total="count">
      </el-pagination>
    </div>

      <!--司机信息-->
      <el-dialog
              :close-on-click-modal="false"
              :beforeClose="handleClose2"
              title="司机信息"
              :visible.sync="dialogVisible2"
              width="500px">
          <el-form :model="sendForm" size="mini" label-width="120px" ref="sendForm" :rules="rules2">
              <el-form-item label="司机姓名：" prop="name">
                  <el-input v-model="sendForm.name" style="width:130px"></el-input>
              </el-form-item>
              <el-form-item label="手机号：" prop="phone">
                  <el-input v-model="sendForm.phone" style="width:130px"></el-input>
              </el-form-item>
              <el-form-item label="车牌号：" >
                  <el-input v-model="sendForm.carNo" style="width:130px"></el-input>
              </el-form-item>
              <el-form-item label="身份证号：" >
                  <el-input v-model="sendForm.idCard" style="width:200px"></el-input>
              </el-form-item>
              <el-form-item label="银行名称：">
                  <el-input v-model="sendForm.bankName" style="width:200px"></el-input>
              </el-form-item>
              <el-form-item label="银行卡号：" >
                  <el-input v-model="sendForm.bankNo" style="width:200px"></el-input>
              </el-form-item>
              <el-form-item label="常跑路线：" >
                  <el-input :autosize="{ minRows: 2, maxRows: 6}" type="textarea" v-model="sendForm.route"></el-input>
              </el-form-item>
              <el-form-item label="备注信息：">
                  <el-input :autosize="{ minRows: 2, maxRows: 6}" type="textarea" v-model="sendForm.remark"></el-input>
              </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose2">取 消</el-button>
          <el-button type="primary" @click="handleConfirm2" :loading="btnLoading2">确 定</el-button>
      </span>
      </el-dialog>

  </div>
</template>
<script>
  import {getDriversCreate,getDriversDel,getDrivers} from '@/api/partner'
  import {formatDate} from '@/utils/date';
  import {validatePhone} from '@/utils/validate';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  const defaultListQuery={
    pageNum: 1,
    pageSize: 10,
    keywords: '',
  };
  const defaultSendForm={
      driverId:null,
      name: '',
      phone: [],
      carNo: '',
      idCard:'',
      bankName:'',
      bankNo:'',
      route:'',
      remark:'',
  };
  export default {
    name: "index",
    data(){
        const isvalidatePhone = (rule, value, callback) => {
            if(!validatePhone(value)){
                callback(new Error('请输入正确的手机号'))
            } else {
                callback()
            }
        };
      return {
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        tableData: [],
        count: 0,
        listLoading: false,
        roles:store.getters.roles ? store.getters.roles : [],
        dialogVisible2:false,
        btnLoading2:false,
        sendForm:JSON.parse(JSON.stringify(defaultSendForm)),
        rules2:{
            name:[{required: true, trigger: 'blur' ,message: '请输入司机姓名',}],
            phone:[{required: true, trigger: 'blur' ,validator: isvalidatePhone,}],

        },
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId'
      ]),

    },
    created() {
      this.getList();
    },
    filters: {
      formatCreateTime(time) {
        if(time){
          let date = new Date(time);
          return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
        }else{
          return '--'
        }
      },
    },
    methods:{
        delDate(item){
            this.$confirm('是否确定删除', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                getDriversDel({driverId:item.driverId}).then(()=>{
                    this.getList();
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });

                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                });
            });
        },

        //显示司机
        showSendForm(type,item){
            if(type=='edit'){
                this.sendForm.driverId = item.driverId;
                this.sendForm.name = item.name;
                this.sendForm.phone = item.phone;
                this.sendForm.carNo = item.carNo;
                this.sendForm.idCard = item.idCard;
                this.sendForm.bankName = item.bankName;
                this.sendForm.bankNo = item.bankNo;
                this.sendForm.route = item.route;
                this.sendForm.remark = item.remark;
            }else{
                this.sendForm = JSON.parse(JSON.stringify(defaultSendForm));
            }
            this.dialogVisible2 = true;
        },

        //关闭司机
        handleClose2(){
            this.dialogVisible2 = false;
            this.$refs.sendForm.clearValidate();
        },

        //提交司机信息
        handleConfirm2(){
            this.$refs.sendForm.validate((valid) => {
                if (valid) {
                    this.btnLoading2 = true;
                    let data = JSON.parse(JSON.stringify(this.sendForm));
                    getDriversCreate(data).then(()=>{
                        this.btnLoading2 = false;
                        this.handleClose2();
                        this.getList();
                    }).catch(()=>{
                        this.btnLoading2 = false;
                    })
                }
            })
        },

      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },

      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
      },

      //获取发货列表
      getList() {
        this.listLoading = true;
          getDrivers(this.listQuery).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },

    },
  }
</script>

<style lang="scss">
</style>
